package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.system.common.result.Result;
import com.system.runtai.entity.*;
import com.system.runtai.service.*;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 首页 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-04-11
 */
@Api(tags = "首页")
@RestController
@RequestMapping("/runtai/home-page")
public class HomePageController {


    @Autowired
    private HomePageService homePageService;

    @Autowired
    private CarService carService;

    @Autowired
    private IpaEnterService ipaEnterService;

    @Autowired
    private ContactUsService contactUsService;

    @Autowired
    private NeedService needService;

    @Autowired
    private AppletUserService appletUserService;

    @Autowired
    private CompanyAnnouncementsService companyAnnouncementsService;

    @ApiOperation(value = "首页内容")
    @GetMapping("/homepage")
    public Result homePage(){

        HomePage homePage = new HomePage();
        LambdaQueryWrapper<HomePage> wrapper5 = new LambdaQueryWrapper<>();
        wrapper5.eq(HomePage::getStart,1);
        HomePage list = homePageService.getOne(wrapper5);
        homePage.setPicture(list.getPicture());

        LambdaQueryWrapper<CompanyAnnouncements> companyAnnouncementsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        companyAnnouncementsLambdaQueryWrapper.orderByDesc(CompanyAnnouncements::getId);
        List<CompanyAnnouncements> list1 = companyAnnouncementsService.list(companyAnnouncementsLambdaQueryWrapper);
        for (int i = 0; i < list1.size(); i++) {
            CompanyAnnouncements companyAnnouncements = list1.get(0);
            homePage.setCa(companyAnnouncements.getTitle());

        }


        LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Car::getNumber);
        List<Car> listCar = carService.list(wrapper);
        //车辆浏览记录由高到低排列
        homePage.setCarList(listCar);

        List<IpaEnter> listIpa = ipaEnterService.list();
//        String s = AuthContextUtil.get();
//
//        LambdaQueryWrapper<AppletUser> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(AppletUser::getAvatar,Integer.valueOf(s));
//        AppletUser one = appletUserService.getOne(queryWrapper);
        //行业通讯录


        homePage.setIpaList(listIpa);

        List<Need> listNeed = needService.list();
        //需求专区
        homePage.setNeedList(listNeed);

        List<ContactUs> contactUsList = contactUsService.list();
        for (int i = 0; i < contactUsList.size(); i++) {
            ContactUs contactUs = contactUsList.get(i);
            //联系我们
            homePage.setContactUs(contactUs);
        }

        return Result.ok(homePage);
    }

//    @ApiOperation(value = "添加首页轮播图")
//    @PostMapping("/saveHomePage")
//    public Result saveHomePage(@RequestBody HomePage homePage){
//        boolean save = homePageService.save(homePage);
//
//        if (save){
//            return Result.ok().message("添加成功");
//        }
//        return Result.ok().message("添加失败");
//    }

    @ApiOperation(value = "修改首页轮播图")
    @PostMapping("/updateHomePage")
    public Result updateHomePage(@RequestBody HomePage homePage){


        homePage.setUpdateTime(LocalDateTime.now());
        boolean update = homePageService.updateById(homePage);

        if (update){
            return Result.ok(homePage).message("修改成功");
        }else {
            return Result.fail(homePage).message("修改失败");
        }
    }

    @ApiOperation(value = "后台：首页轮播图")
    @GetMapping("/selectHomePage")
    public Result selectHomePage(){

        List<HomePage> list = homePageService.list();
        return Result.ok(list);
    }

}

